package com.tanhua.manage.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.manage.mapper.PublishInfoMapper;
import com.tanhua.manage.pojo.PublishInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author guzi
 * @date 2021/1/12
 */

@Service
@Slf4j
public class PublishInfoService {

    @Autowired
    private PublishInfoMapper publishInfoMapper;


    /**
     * 根据查询参数查询动态列表
     *
     * @param param
     */
    public IPage<PublishInfo> queryPublishInfoListByParam(Map<String, Object> param) {
        //参数解析
        Integer page = (Integer) param.get("page");
        Integer pagesize = (Integer) param.get("pagesize");
        String sortProp = (String) param.get("sortProp");
        String sortOrder = (String) param.get("sortOrder");
        String sd = (String) param.get("sd");
        String ed = (String) param.get("ed");
        String state = (String) param.get("state");
        String publishId = (String) param.get("id");

        System.out.println(page);
        System.out.println(pagesize);
        System.out.println(sortProp);
        System.out.println(sortOrder);
        System.out.println(sd);
        System.out.println(ed);
        System.out.println(state);
        System.out.println(publishId);


        //构造分页条件
        IPage<PublishInfo> publishInfoPage = new Page<>(page, pagesize);

        //构造查询条件
        QueryWrapper<PublishInfo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(publishId)) {
            System.out.println("001");
            wrapper = wrapper.eq("publish_id", publishId);
        }
        if (!"-1".equals(sd)) {
            System.out.println("002");
            wrapper = wrapper.gt("create_Date", sd);
        }
        if (!"-1".equals(ed)) {
            System.out.println("003");
            wrapper = wrapper.lt("create_Date", ed);
        }
        if (!"all".equals(state)) {
            System.out.println("004");
            wrapper = wrapper.eq("state", state);
        }
        if ("ascending".equals(sortOrder)) {
            System.out.println("005");
            wrapper = wrapper.orderByAsc("create_date");
        } else if ("descending".equals(sortOrder)) {
            System.out.println("006");
            wrapper = wrapper.orderByDesc("create_date");
        }

        //查询
        IPage<PublishInfo> publishInfoIPage = this.publishInfoMapper.selectPage(publishInfoPage, wrapper);
        System.out.println(publishInfoIPage.getRecords());
        return publishInfoIPage;
    }

    /**
     * 根据 publishIds 和 State 通过、拒绝、撤销 人工审核
     * 审核状态，1为待审核，2为自动审核通过，3为待人工审核，4为人工审核拒绝，5为人工审核通过，6为自动审核拒绝
     *
     * @param publishIds
     * @return
     */
    public String publishAuditByPublishIdsAndState(String[] publishIds, String s) {
        //构造查询条件
        QueryWrapper<PublishInfo> wrapper = new QueryWrapper<>();
        wrapper.in("publish_id", publishIds);
        PublishInfo publishInfo = new PublishInfo();
        if (s.equals(PublishAuditService.PASS)) {
            publishInfo.setState("5");
        } else if (s.equals(PublishAuditService.REJECT)) {
            publishInfo.setState("4");
        } else if (s.equals(PublishAuditService.REVOCATION)) {
            publishInfo.setState("3");
        } else {
            return null;
        }

        //TODO 记录更新到日志

        //查询结果并返回
        return this.publishInfoMapper.update(publishInfo, wrapper) + "";
    }

    public Integer queryPublishByState(String state) {
        QueryWrapper<PublishInfo> wrapper = new QueryWrapper<>();
        if ("all" != state) {
            wrapper.eq("state", state);
        }
        return this.publishInfoMapper.selectCount(wrapper);
    }
}
